﻿using MediatR;
using Traceability.API.Applications.Command.TracebackCommand;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.API.Applications.CommandHandler.TracebackCommandHandler
{
    public class TracebackListDelCommandHandler : IRequestHandler<TracebackListDelCommand, APIResult<int>>
    {
        private readonly IBaseRepository<Traceback_Information> tracebackRepository;
        public TracebackListDelCommandHandler(IBaseRepository<Traceback_Information> tracebackRepository)
        {
            this.tracebackRepository = tracebackRepository;
        }


        public async Task<APIResult<int>> Handle(TracebackListDelCommand request, CancellationToken cancellationToken)
        {
            var res = tracebackRepository.GetAll().Where(s=>request.Id.Contains(s.Id));
            var list = new List<Traceback_Information>();

            foreach(var item in res)
            {
                item.IsDeltede = true;
                list.Add(item);
            }

            var count =await tracebackRepository.ListUpdate(list);
            APIResult<int> aPIResult = new APIResult<int>();    
            if (count>0)
            {
                aPIResult.Code = EnumCode.Ok;
                aPIResult.Msg = "修改成功";
                aPIResult.Data = count;
            }
            else
            {
                aPIResult.Code = EnumCode.File;
                aPIResult.Msg = "修改失败";
                aPIResult.Data = count;
            }

            return await Task.FromResult(aPIResult);
        }
    }
}
